package com.mall.admin.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.mall.commons.utils.PageResult;
import com.mall.domain.*;
import com.mall.dto.ProductDto;
import com.mall.dto.ProductListDto;
import com.mall.interface1.api.PmsProductApi;
import com.mall.vo.ProductVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PmsProductService {

    @DubboReference
    private PmsProductApi pmsProductApi;

    //创建商品
    public void create(ProductDto dto) {
        PmsProduct pmsProduct = new PmsProduct();
        BeanUtil.copyProperties(dto,pmsProduct);
        //保存商品基本信息，并返回商品id
        Long productId = pmsProductApi.saveProduct(pmsProduct);
        List<PmsMemberPrice> pmpList = dto.getMemberPriceList();
        if(CollUtil.isNotEmpty(pmpList)){
            for (PmsMemberPrice pmp : pmpList) {
                pmp.setProductId(productId);
                pmsProductApi.savePmp(pmp);
            }
        }

        List<CmsPrefrenceAreaProductRelation> cpaprList = dto.getPrefrenceAreaProductRelationList();
        if(CollUtil.isNotEmpty(cpaprList)){
            for (CmsPrefrenceAreaProductRelation cpapr : cpaprList) {
                cpapr.setProductId(productId);
                pmsProductApi.saveCpapr(cpapr);
            }
        }

        List<PmsProductAttributeValue> ppavList = dto.getProductAttributeValueList();
        if(CollUtil.isNotEmpty(ppavList)){
            for (PmsProductAttributeValue ppav : ppavList) {
                ppav.setProductId(productId);
                pmsProductApi.savePpav(ppav);
            }
        }

        List<PmsProductFullReduction> ppfrList = dto.getProductFullReductionList();
        if(CollUtil.isNotEmpty(ppfrList)){
            for (PmsProductFullReduction ppfr : ppfrList) {
                ppfr.setProductId(productId);
                pmsProductApi.savePpfr(ppfr);
            }
        }

        List<PmsProductLadder> pplList = dto.getProductLadderList();
        if(CollUtil.isNotEmpty(pplList)){
            for (PmsProductLadder ppl : pplList) {
                ppl.setProductId(productId);
                pmsProductApi.savePpl(ppl);
            }
        }

        List<PmsSkuStock> pssList = dto.getSkuStockList();
        if(CollUtil.isNotEmpty(pssList)){
            for (PmsSkuStock pss : pssList) {
                pss.setProductId(productId);
                pmsProductApi.savePss(pss);
            }
        }

        List<CmsSubjectProductRelation> csprList = dto.getSubjectProductRelationList();
        if(CollUtil.isNotEmpty(csprList)){
            for (CmsSubjectProductRelation cspr : csprList) {
                cspr.setProductId(productId);
                pmsProductApi.saveCspr(cspr);
            }
        }

    }

    //查询商品
    public PageResult list(ProductListDto dto) {
        //分页查询出商品信息
        List<PmsProduct> productList = pmsProductApi.list(dto);
        //查询商品总数
        Integer count = pmsProductApi.count(dto);
        return new PageResult(dto.getPageNum(),dto.getPageSize(),count,productList);
    }

    //根据商品名称或货号模糊查询
    public List<PmsProduct> simpleList(String keyword) {
        return pmsProductApi.simpleList(keyword);
    }

    //批量修改删除状态
    public void updateDeleteStatus(List<Long> ids, Integer deleteStatus) {
        if(ids != null){
            for (Long id : ids) {
                //根据id查询商品
                PmsProduct pmsProduct = pmsProductApi.findById(id);
                pmsProduct.setDeleteStatus(deleteStatus);
                //修改商品
                pmsProductApi.update(pmsProduct);
            }
        }
    }

    //批量设为新品
    public void updateNew(List<Long> ids, Integer newStatus) {
        if(ids != null){
            for (Long id : ids) {
                //根据id查询商品
                PmsProduct pmsProduct = pmsProductApi.findById(id);
                pmsProduct.setNewStatus(newStatus);
                //修改商品
                pmsProductApi.update(pmsProduct);
            }
        }
    }

    //批量上下架商品
    public void updatePublish(List<Long> ids, Integer publishStatus) {
        if(ids != null){
            for (Long id : ids) {
                //根据id查询商品
                PmsProduct pmsProduct = pmsProductApi.findById(id);
                pmsProduct.setPublishStatus(publishStatus);
                //修改商品
                pmsProductApi.update(pmsProduct);
            }
        }
    }

    //批量推荐商品
    public void updateRecommend(List<Long> ids, Integer recommendStatus) {
        if(ids != null){
            for (Long id : ids) {
                //根据id查询商品
                PmsProduct pmsProduct = pmsProductApi.findById(id);
                pmsProduct.setRecommandStatus(recommendStatus);
                //修改商品
                pmsProductApi.update(pmsProduct);
            }
        }
    }

    //批量修改审核状态
    public void updateVerify(String detail, List<Long> ids, Integer verifyStatus) {
        if(ids != null){
            for (Long id : ids) {
                //根据id查询商品
                PmsProduct pmsProduct = pmsProductApi.findById(id);
                pmsProduct.setVerifyStatus(verifyStatus);
                //修改商品
                pmsProductApi.update(pmsProduct);
            }
        }
    }

    //更新商品
    public void update(ProductDto dto) {
        PmsProduct pmsProduct = new PmsProduct();
        BeanUtil.copyProperties(dto,pmsProduct);
        //修改产品
        pmsProductApi.update(pmsProduct);
        //修改其他关联表
        if(CollUtil.isNotEmpty(dto.getMemberPriceList())){
            for (PmsMemberPrice pmp : dto.getMemberPriceList()) {
                pmsProductApi.updatePmp(pmp);
            }
        }
        if(CollUtil.isNotEmpty(dto.getPrefrenceAreaProductRelationList())){
            for (CmsPrefrenceAreaProductRelation cpapr : dto.getPrefrenceAreaProductRelationList()) {
                pmsProductApi.updateCpapr(cpapr);
            }
        }
        if(CollUtil.isNotEmpty(dto.getProductAttributeValueList())){
            for (PmsProductAttributeValue ppav : dto.getProductAttributeValueList()) {
                pmsProductApi.updatePpav(ppav);
            }
        }
        if(CollUtil.isNotEmpty(dto.getProductFullReductionList())){
            for (PmsProductFullReduction ppfr : dto.getProductFullReductionList()) {
                pmsProductApi.updatePpfr(ppfr);
            }
        }
        if(CollUtil.isNotEmpty(dto.getProductLadderList())){
            for (PmsProductLadder ppl : dto.getProductLadderList()) {
                pmsProductApi.updatePpl(ppl);
            }
        }
        if(CollUtil.isNotEmpty(dto.getSkuStockList())){
            for (PmsSkuStock pss : dto.getSkuStockList()) {
                pmsProductApi.updatePss(pss);
            }
        }
        if(CollUtil.isNotEmpty(dto.getSubjectProductRelationList())){
            for (CmsSubjectProductRelation cspr : dto.getSubjectProductRelationList()) {
                pmsProductApi.updateCspr(cspr);
            }
        }
    }

    //根据商品id获取商品编辑信息
    public ProductVo updateInfo(Long id) {
        ProductVo vo = new ProductVo();
        PmsProduct pmsProduct = pmsProductApi.findById(id);
        List<PmsMemberPrice> memberPriceList = pmsProductApi.findPmp(id);
        List<CmsPrefrenceAreaProductRelation> prefrenceAreaProductRelationList = pmsProductApi.findPprl(id);
        List<PmsProductAttributeValue> productAttributeValueList = pmsProductApi.findPavl(id);
        List<PmsProductFullReduction> productFullReductionList = pmsProductApi.findPfrl(id);
        List<PmsProductLadder> productLadderList = pmsProductApi.findPll(id);
        List<PmsSkuStock> skuStockList = pmsProductApi.findSsl(id);
        List<CmsSubjectProductRelation> subjectProductRelationList = pmsProductApi.findSprl(id);
        BeanUtil.copyProperties(pmsProduct,vo);
        vo.setMemberPriceList(memberPriceList);
        vo.setPrefrenceAreaProductRelationList(prefrenceAreaProductRelationList);
        vo.setProductAttributeValueList(productAttributeValueList);
        vo.setProductFullReductionList(productFullReductionList);
        vo.setProductLadderList(productLadderList);
        vo.setSkuStockList(skuStockList);
        vo.setSubjectProductRelationList(subjectProductRelationList);
        return vo;
    }
}
